package com.ninong.ker.admin.rest;

import java.util.List;

import com.ninong.ker.admin.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ninong.ker.admin.domain.Role;
import com.ninong.ker.admin.service.dto.RoleDTO;
import com.ninong.ker.common.annotation.log.Log;
import com.ninong.ker.common.annotation.perm.CmdAdmin;
import com.ninong.ker.common.response.CmdResponse;
import com.ninong.ker.common.tools.enums.BusinessEnum;

/**
 * 角色资源
 * @author cmd
 *
 */
@RestController
@RequestMapping("/cmd/v1/role")
public class RoleResouces {
	
	@Autowired
	private RoleService roleService;
	
	@Log("角色列表")
	@CmdAdmin({"admin","role:list"})
	@PostMapping
	public ResponseEntity<CmdResponse> list(@RequestBody(required = false) RoleDTO dto)  {
		CmdResponse response = new CmdResponse();
		response.setData(roleService.listPage(dto));
		return ResponseEntity.ok(response);
	}
	
	@Log("角色删除")
	@CmdAdmin({"admin","role:delete"})
	@DeleteMapping
	public ResponseEntity<CmdResponse> delete(@RequestBody String[] ids)  {
		CmdResponse response = new CmdResponse();
		roleService.delete(ids);
		response.setMessage(BusinessEnum.DELETE_SUCCESS.getMsg());
		return ResponseEntity.ok(response);
	}
	
	@Log("角色新增或编辑")
	@CmdAdmin({"admin","role:add","role:update"})
	@PutMapping
	public ResponseEntity<CmdResponse> create(@RequestBody Role entity)  {
		roleService.create(entity);
		return ResponseEntity.ok(new CmdResponse());
	}
	
	@Log("角色详情")
	@GetMapping(value = "/{id}")
	public ResponseEntity<CmdResponse> get(@PathVariable String id)  {
		CmdResponse response = new CmdResponse();
		response.setData(roleService.get(id));
		return ResponseEntity.ok(response);
	}
	
	@Log("获取角色树形")
	@GetMapping(value = "/roleTree")
	public ResponseEntity<CmdResponse> tree()  {
		CmdResponse response = new CmdResponse();
		response.setData(roleService.listRoleTree());
		return ResponseEntity.ok(response);
	}
	
	@Log("获取已选中数据")
	@GetMapping(value = "/tree/{userId}")
	public ResponseEntity<CmdResponse> tree(@PathVariable String userId)  {
		CmdResponse response = new CmdResponse();
		response.setData(roleService.listTreeByUserId(userId));
		return ResponseEntity.ok(response);
	}
	
	@Log("更新用户角色关系")
	@PutMapping(value = "/merge/{userId}")
	public ResponseEntity<CmdResponse> merge(@PathVariable String userId,@RequestBody List<String> roleIds)  {
		roleService.merge(userId,roleIds);
		return ResponseEntity.ok(new CmdResponse());
	}

}
